package com.endava.nio_io.exercises;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ShapeUtil {

	public void writeShapes(Shape shape) {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;

		try {
			fos = new FileOutputStream("objects.dat");
			oos = new ObjectOutputStream(fos);
			oos.writeObject(shape);
		} catch (FileNotFoundException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			try {
				oos.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}

	}

	public Shape readShapes() {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		Shape shape = null;
		try {
			fis = new FileInputStream("objects.dat");
			ois = new ObjectInputStream(fis);
			shape = (Shape) ois.readObject();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return shape;
	}
}
